/**
 * Copyright (C), 2021, 因特驰
 * FileName: AdminController
 * Author:   李晋武
 * Date:     2021/7/13 23:26
 * Description:
 * History:
 * 李晋武          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.esc.controller;

import com.esc.model.Admin;
import com.esc.model.Role;
import com.esc.service.AdminService;
import com.esc.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * 〈操作员〉<br>
 * 〈〉
 *
 * @author Yun
 * @create 2021/7/13
 * @since 1.0.0
 */
@Controller
@RequestMapping("admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    /*
    登陆开始
     */
    //加载验证码
    @RequestMapping("imgcode")
    public String imgcode(HttpServletRequest request,HttpServletResponse response){
        String checkImgPath = "";
        try {
            checkImgPath = CheckImgUtil.checkImg(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return checkImgPath;
    }

    //登录
    @RequestMapping("login")
    @ResponseBody
    public Map<String,String> login(String checkCode, Admin admin, HttpServletRequest request){
        Map<String,String> map = new HashMap<String,String>();
        //校验验证码是否正确
        if(!request.getSession().getAttribute("checkcode").equals(checkCode)){
            map.put("code","1");
            map.put("msg","验证码不正确，请重新输入");
            return map;
        }
        //校验当前用户是否存在
        Admin adminRest = adminService.queryAdminByName(admin.getAdminname());
        if(adminRest==null){
            map.put("code","2");
            map.put("msg","用户名或密码不正确，请重新输入");
            return map;
        }
        //校验密码是否正确(（（用户名+密码）16+盐（用户名+注册时间））32)
        String inputPwd = Md5Util.getMd532(Md5Util.getMd516(admin.getAdminname()+admin.getAdminpwd())+adminRest.getSale()).substring(3,16).replace("-","");
        String getPwd = adminRest.getAdminpwd();
        if (!getPwd.equals(inputPwd)){
            map.put("code","2");
            map.put("msg","用户名或密码不正确，请重新输入");
            return map;
        }
        map.put("code","0");
        request.getSession().setAttribute("adminimg",adminRest.getAdminimg());
        request.getSession().setAttribute("adminname",adminRest.getAdminname());
        request.getSession().setAttribute("rolename",adminRest.getRole().getRolename());
        return map;
    }

    //获取当前登录的操作员
    @RequestMapping("getAdmin")
    @ResponseBody
    public Map<String,String> getAdmin(HttpServletRequest request){
        Map<String,String> map = new HashMap<String,String>();
        map.put("adminimg", (String) request.getSession().getAttribute("adminimg"));
        map.put("adminname", (String) request.getSession().getAttribute("adminname"));
        map.put("rolename", (String) request.getSession().getAttribute("rolename"));
        return map;
    }
    /*
    登陆结束
     */

    /*
    注销用户开始
     */
    @RequestMapping("exit")
    @ResponseBody
    public void exit(HttpServletRequest request){
        request.getSession().invalidate();
    }
    /*
    注销用户结束
     */

    /*
    注册开始
     */
    //去注册操作员
    @RequestMapping("/toReg")
    public String toReg(){
        return "admin/regAdmin";//注意此处不要写成 ../admin/regAdmin
    }

    //校验用户是否存在
    @RequestMapping("isNullByName")
    @ResponseBody
    public Boolean isNullByName(String adminname){
        Admin admin = adminService.queryAdminByName(adminname);
        if (admin==null){
            return true;
        }
        return false;
    }

    //添加操作员
    @RequestMapping("addAdmin")
    @ResponseBody
    public void addAdmin(Admin admin, Integer roleid){
        Role role = new Role();
        role.setRoleid(roleid);
        admin.setRole(role);
        //一次加密
        String md516 = Md5Util.getMd516(admin.getAdminname()+admin.getAdminpwd());
        //添加新盐
        admin.setSale(admin.getAdminname()+System.currentTimeMillis());
        //二次加密
        String md532 = Md5Util.getMd532(md516+admin.getSale());
        //加密后字符串处理
        String newPwd = md532.substring(3,16).replace("-","");
        admin.setAdminpwd(newPwd);
        adminService.addAdmin(admin);
    }
    /*
    注册结束
     */

    /*
    查询开始
     */
    //去查询卖车用户集
    @RequestMapping("showAdmin")
    public String showAdmin(Integer queryId, Model model){
        model.addAttribute("queryId",queryId);
        return "admin/showAdmin";
    }
    //查询卖车用户列表
    @RequestMapping("queryAdminList")
    @ResponseBody
    public PageResult queryAdminList(PageUtil<Admin> pageUtil, Admin admin, Integer queryId){
        return adminService.queryAdminList(pageUtil,queryId);
    }

    /*
    查询结束
     */

}